//
//  ST_ProfileBirthdayController.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/8.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import RealmSwift
import SwifterSwift

class ST_ProfileBirthdayController: UIViewController,ST_RegisterRouteProtocol{

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = Color.white
        //RegisterStepManager.shared.step = 5
        view.addSubview(icon_image_view)
        icon_image_view.frame = CGRect.init(x: ScreenWidth, y: 73-20+statusBarHeight, width: 24, height: 24)
        view.addSubview(birth_day_sub_label)
        birth_day_sub_label.frame = CGRect.init(x: ScreenWidth + 10, y: 73-20+statusBarHeight, width: 200, height: 50)
        
        
        
        view.addSubview(birth_day_btn)
        birth_day_btn.frame = CGRect.init(x: ScreenWidth, y: birth_day_sub_label.frame.maxY + 101, width: 200, height: 30)
        
        
        view.addSubview(next_btn)
        next_btn.frame = CGRect.init(x: ScreenWidth - 45 - 40, y: birth_day_btn.frame.maxY + 109, width: 45, height: 45)
        next_btn.alpha = 0.1
        
       
        
        guard let account = ST_RegisterMonitor.shared.regist_response_account()?.userinfo else{
            return
        }
        
        birth_day_sub_label.text = "嗨，"+account.nickname+"\n你的生日是哪天？"
            
        
//        TianRegisterStepManager.shared.pushStepLog(stepInt: 5)
        
        
        self.birthday_pick_action(date: self.minimum_birhday)
        
        //TianRegisterStepManager.shared.addHelperBtn(root_view: view)
    }
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
    
        UIView.zenly_animate(withDelay: 0.25, animations: {
            
            self.icon_image_view.x = 30
            
            self.birth_day_sub_label.x = 60
            
        }) { (f) in
            
            
            UIView.zenly_animate(withDelay: 0, animations: {

                self.birth_day_btn.center = CGPoint.init(x: ScreenWidth/2.0, y:             self.birth_day_btn.center.y)
                
            }) { (f) in
                
                UIView.zenly_animate(animations: {
                    
                    self.next_btn.alpha = 1
                    
                }) { (f) in
                    
                    self.select_birth_day_action()
                    
                }
                
            }
        }
    }
    

//    MARK: - date picker
    
    lazy var pickerView:ST_BirthdayPickerView = {
            
        let control = ST_BirthdayPickerView()
        
        control.confirm_pick_closure = {[weak self] date in
            self?.birthday_pick_action(date: date)
        }
        return control
    }()
    
//    MARK: - Properties
    lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("register_icon_emoji生日")
        return control
    }()
    

   lazy var birth_day_sub_label:UILabel = {
       let control = UILabel.init()
       control.textColor = Color.init(hexString: "#333333")
       control.numberOfLines = 0
       control.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
       return control
   }()
    
    
    lazy var next_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)

        control.setImageForAllStates(ST_ImageUtils.imageName("register_bot_下一步")!)
        
        control.setImage(ST_ImageUtils.imageName("register_bot_下一步diss"), for: UIControl.State.disabled)
        
        control.addTarget(self, action: #selector(next_action), for: UIControl.Event.touchUpInside)
        
        control.isEnabled = false
        
        control.alpha = 0
        
        return control
    }()
    
    lazy var birth_day_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("请选择生日")
        control.setTitleColorForAllStates(Color.init(hexString: "#999999")!)
        control.setTitleColor(Color.init(hexString: "#333333"), for: UIControl.State.selected)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(select_birth_day_action), for: UIControl.Event.touchUpInside)
        
//        control.contentHorizontalAlignment = .left
//        control.backgroundColor = Color.random
        
        return control
    }()
    
    

    //    MARK: - Event
    var next_action_closure:(()->())?
    
    @objc func next_action(){
        
        
        
       let birthString = (select_date as NSString).replacingOccurrences(of: "/", with: "-")
        
        PlayFeedBack()
        
        self.pushNextRegisterController(parameters: ["birth" : birthString])
      
        
    }
    
    
    @objc func birthday_pick_action(date:Date){
        
        let formatter = DateFormatter()
        //日期样式
        formatter.dateFormat = "yyyy/MM/dd"
        
        self.select_date =  formatter.string(from: date)
        
        self.birth_day_btn.setTitleForAllStates(self.select_date)
        
        self.birth_day_btn.isSelected = true
        
        self.next_btn.isEnabled = true
        
    }
    
    @objc func select_birth_day_action(){
        
        if self.pickerView.superview == nil {
            
            self.view.addSubview(self.pickerView)
            pickerView.snp.makeConstraints { (make) in
                make.left.bottom.right.equalTo(self.view)
                make.height.equalTo(ST_BirthdayPickerView.height)
            }
           
            
        }
        
        if let date = self.select_date.date(withFormat: "yyyy/MM/dd"){
            self.pickerView.setBirthDay(date: date)
        }
        
        self.pickerView.isHidden = false
        
    }
    
    /**
     注册用户最小17岁
     */
    lazy var minimum_birhday:Date = {
        let date = Date().adding(Calendar.Component.year, value: -18)
        return date
    }()
    
    lazy var select_date:String = {
        
        let formatter = DateFormatter()
        //日期样式
        formatter.dateFormat = "yyyy/MM/dd"
        
        let str = formatter.string(from: minimum_birhday)
        
        return str
    }()
    
}
